<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Task;
use App\Models\TaskProcess;
use App\Repos\TaskRepo;
use Log,Session,DB;
use App\Services\Wechat;

class IndexController extends Controller
{
    public function index()
    {
    	$tasks =Task::whereIn('status',['1','50','75'])->orderBy('created_at','desc')->take(10)->get();

        $where = [];
        
        $where['category_id'] = 1;
        $cate1    = Task::where($where)->count();
        $where['category_id'] = 2;
        $cate2 = Task::where($where)->count();
       

        $workers = User::where('role_code',1)->get();
    	
    	return view('admin.index',compact('data','tasks','cate1','cate2'));
    }

    public function refund(Request $request)
    {
        $rs['status'] = 'danger';
        $rs['msg'] = '操作失败';
        $data = $request->all();
        $data['username'] = session('admin')->username.'('.session('admin')->truename.')';
        $flag = TaskRepo::refund($data);       
        if($flag['status']==true) {
            $rs['status'] = 'success';
            $rs['msg']    = '操作成功';
        }

        return back()->with('rs',$rs);

    }

    /**
     * 派工
     */
    public function process(Request $request)
    {
        
        $rs['status'] = 'success';
        $rs['msg']    = '操作成功';
        $status = 50;
        $data = $request->except(['_token']);        
        $data['status'] = $status;
        $flag   = TaskProcess::create($data);
        $task   = Task::find($data['task_id']);
        $task->status    = $status;
        $task->worker_id = $data['worker_id'];
        $task->save();
        $worker = User::find($data['worker_id']);
        $msg = $task->toArray();
        $msg['first']     = '有新的工单等待处理';
        $msg['openid']    = $worker->openid;
        $msg['cate_name'] = object_get($task,'category.name');
        $msg['address']   = object_get($task,'depart.name').','.$msg['gatenum'].','.$task->truename.','.$task->mobile;
        TaskRepo::sendDispatchMsg($msg);
        if(!$flag) {
            $rs['status'] = 'danger';
            $rs['msg']    = '操作失败';
            return back()->with('rs',$rs);
        }
        return redirect('zadmin/task/')->with('rs',$rs);
        
    }

    public function test()
    {
        $system = DB::table('systems')->first();

        if(object_get($system,'work_time')) {
            $work_time = explode('-', $system->work_time);

            $rs = $this->checkIsBetweenTime($work_time[0],$work_time[1]);
            dd($rs);
        }
        
    }


    public function checkIsBetweenTime($start,$end){
        $result = false;
        $date= date('H:i');
        $curTime = strtotime($date);//当前时分
        $assignTime1 = strtotime($start);//获得指定分钟时间戳，00:00
        $assignTime2 = strtotime($end);//获得指定分钟时间戳，01:00
        $result = 0;
        if($curTime>$assignTime1&&$curTime<$assignTime2){
            $result = true;
        }
        return $result;
    }


}
